package com.sdry.web.controller.td;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sdry.base.BaseController;
import com.sdry.entity.base.Staff;
import com.sdry.entity.td.BillForm;
import com.sdry.entity.td.BillFormExample;
import com.sdry.entity.td.BillFormExample.Criteria;
import com.sdry.service.td.BillFormServise;
import com.sdry.utils.ResponseUtil;
import com.sdry.utils.StringUtil;


/**
 * @ClassName: billFormController
 * @Description: 模板
 * @author tdd
 * @date 2018年8月29日
 * @version V1.0
 */
@Controller
@RequestMapping("/billForm")
public class BillFormController extends BaseController{

    @Resource
    private BillFormServise billFormServise;


    /**
     * 去模板列表页面
     * @return
     */
    @RequestMapping("/pageTobillForm")
    public String pageTobillForm(Model model ,String bfremark12) {
    	if(StringUtil.isNotEmpty(bfremark12)){
    		model.addAttribute("bfremark12",bfremark12);
    	}
        return "/billForm/billForm";
    }

    /**
     * 去模板详情页面
     * @return
     */
    @RequestMapping("/pageTobillFormXq")
    public String pageTobillFormXq(Model model,Long id) {
        if (null != id && id != 0){
            BillForm billForm = billFormServise.selectByPrimaryKey(id);
            model.addAttribute("billForm",billForm);
        }
        return "/billForm/billFormXq";
    }

    /**
     * 添加模板
     * @param response 响应
     * @param billForm 模板实体
     * @throws Exception 异常
     */
    @RequestMapping("/insertbillForm")
    public void insertbillForm(HttpServletRequest request, HttpServletResponse response, BillForm billForm) throws Exception {

        int affact = 0;

        //添加
        affact = billFormServise.insertSelective(billForm);
        ResponseUtil.write(response, affact);
    }

    /**
     * 带条件的分页查询模板信息
     * @param request 请求
     * @param response 响应
     * @param rows 每页记录数
     * @param page 页数
     * @param billForm 模板信息实体
     * @throws Exception 异常
     */
    @RequestMapping("/selectbillFormList")
    public void selectbillFormList(HttpServletRequest request, HttpServletResponse response,
                                   @RequestParam(value = "page",required = false)int page,
                                   @RequestParam(value = "rows",required = false)int rows,
                                   BillForm billForm) throws Exception {

    	PageHelper.startPage(page, rows);

        BillFormExample example = new BillFormExample();
        Criteria createCriteria =  example.createCriteria();
    	//名称模糊
        if(billForm.getBillFormType() != null && billForm.getBillFormType() != "") {
    		createCriteria.andBillFormTypeLike("%"+billForm.getBillFormType()+"%");
    	}
    	//预览提示模糊
        if(billForm.getBillFormName() != null && billForm.getBillFormName() != "") {
            createCriteria.andBillFormNameLike("%"+billForm.getBillFormName()+"%");
        }
        if(billForm.getBfremark12() != null && billForm.getBfremark12() != "") {
        	createCriteria.andBfremark12EqualTo(billForm.getBfremark12());
        }
		List<BillForm> list = billFormServise.selectByExample(example);
		
		PageInfo<BillForm> info = new PageInfo<BillForm>(list);
    	
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("total", info.getTotal());
        jsonObject.put("rows", info.getList());
        ResponseUtil.write(response, jsonObject);
    }

    /**
     * 根据条件查询模板信息
     * @param response 响应
     * @param billForm 条件
     * @throws Exception 异常
     */
    @RequestMapping("/selectbillFormByExample")
    public void selectbillFormByExample(HttpServletResponse response, BillForm billForm) throws Exception {
        BillFormExample example = new BillFormExample();
        Criteria createCriteria =  example.createCriteria();
        //名称模糊
        if(billForm.getBillFormType() != null && billForm.getBillFormType() != "") {
    		createCriteria.andBillFormTypeEqualTo(billForm.getBillFormType());
    	}
    	//预览提示模糊
        if(billForm.getBillFormName() != null && billForm.getBillFormName() != "") {
            createCriteria.andBillFormNameEqualTo(billForm.getBillFormName());
        }
        List<BillForm> billForms = billFormServise.selectByExample(example);
        //将对象转化为json
        //JSONObject json = JSONObject.fromObject(billForms);
        JSONArray json = JSONArray.fromObject(billForms);
        ResponseUtil.write(response, json);
    }
    /**
     * 根据id查询模板信息
     * @param response 响应
     * @param id 模板id
     * @throws Exception 异常
     */
    @RequestMapping("/selectbillFormById")
    public void selectbillFormById(HttpServletResponse response, Long id) throws Exception {

        BillForm billForm = billFormServise.selectByPrimaryKey(id);
        //将对象转化为json
        JSONObject json = JSONObject.fromObject(billForm);
        ResponseUtil.write(response, json);
    }

    /**
     * 修改模板信息
     * @param response 响应
     * @param billForm 模板信息
     * @throws Exception 异常
     */
    @RequestMapping("/editbillForm")
    public void editbillForm( HttpServletResponse response,
                              BillForm billForm) throws Exception {

        int affct = 0;

        //修改
        affct = billFormServise.updateByPrimaryKeySelective(billForm);
        ResponseUtil.write(response, affct);
    }

    /**
     * 批量删除
     * @param response 响应
     * @param ids 模板id
     * @throws Exception 异常
     */
    @RequestMapping("/deletebillForm")
    public void deletebillForm( HttpServletResponse response, String ids) throws Exception {

        int affact = billFormServise.deleteByPrimaryKeys(ids);
        ResponseUtil.write(response, affact);
    }

}
